package v3

import (
	"context"
	"github.com/wechatpay-apiv3/wechatpay-go/core"
	"github.com/wechatpay-apiv3/wechatpay-go/core/option"
	"github.com/wechatpay-apiv3/wechatpay-go/utils"
	"github.com/zeromicro/go-zero/core/logx"
	"xialou_platform/app/errorx"
	"xialou_platform/services/order/api/internal/config"
)

func NewWxPayClientV3(c config.WxPayConf) (*core.Client, error) {

	mchPrivateKey, err := utils.LoadPrivateKeyWithPath(c.PrivateKeyPath)
	if err != nil {
		logx.Errorf(" 微信支付初始化 wx pay client 失败 ，mchPrivateKey err : %v \n", err)
		return nil, errorx.NewDefaultError("微信支付失败")
	}

	ctx := context.Background()

	opts := []core.ClientOption{
		option.WithWechatPayAutoAuthCipher(c.MchId, c.SerialNo, mchPrivateKey, c.APIv3Key),
	}
	client, err := core.NewClient(ctx, opts...)
	if err != nil {
		logx.Errorf("new wechat pay client err: %s", err.Error())
		return nil, errorx.NewDefaultError("微信支付失败")
	}

	return client, nil
}
